package Demo3;

import java.util.Objects;

class Person {
    String name = "hello";
    String id;

    public Person(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override   //箭头向上就是重写
    public boolean equals(Object obj) {
        if (obj == null) {
            //判断obj参数是否为空
            return false;
        }
        /**
         * Person person1 = new Person("123");
         * Person person2 = person1;
         * 专门拿来判断这种传两个参数，实际是指向一个地方
         */
        if (this == obj){
            return true;
        }
        //万一传过来的不是person对象
        if(!(obj instanceof Person)){
            return false;
        }
        Person tmp = (Person) obj;
        return this.id.equals(tmp.id);  //字符串比较 还是要加equals
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id);
    }
}

class Student{

}

public class Test {
    public static void func(Object obj){
        System.out.println(obj);
    }

    public static void main(String[] args) {
        Person person1 = new Person("123");
        Person person2 = new Person("123");
          //System.out.println(person1==person2);   //比的地址
        System.out.println(person1.equals(person2));
        System.out.println();
    }
}
